package utility;

public final class PalindromeChecker {

	private static final int SMALLEST_POSITIVE_SINGLE_DIGIT = 9;
	private static int palindromeLength;
	private static boolean isPalindrome;
	private static String aPossiblePalindrome;
	private static int index;
	private static int numberToCheck;
	private static boolean solvedAfterSingleDigitCheck;

	public static boolean isPalindrome(int numToCheck) {

		numberToCheck = numToCheck;
		solvedAfterSingleDigitCheck = false;

		handleNegativeNumbers();
		handleSingleDigitNumbers();
		ifNotSolvedYetCheckCorrespondingCharacters();

		return isPalindrome;
	}

	private static void handleNegativeNumbers() {
		numberToCheck = Math.abs(numberToCheck);
	}

	private static void handleSingleDigitNumbers() {
		if (numberToCheckIsSingleDigit()) {
			isPalindrome = true;
			solvedAfterSingleDigitCheck = true;
		}
	}

	private static void ifNotSolvedYetCheckCorrespondingCharacters() {
		if (!solvedAfterSingleDigitCheck) {
			initializeMembers();
			loopOverStringAndTryToMatchCorrespondingChars();
		}
	}

	private static void initializeMembers() {
		aPossiblePalindrome = convertIntToString(numberToCheck);
		palindromeLength = aPossiblePalindrome.length();
		isPalindrome = true;
	}

	private static void loopOverStringAndTryToMatchCorrespondingChars() {
		index = 0;
		while (noMisMatchFoundAndEndOfSearchNotReached()) {
			checkIfCorrespondingCharsMatch();
			index++;
		}
	}

	private static boolean noMisMatchFoundAndEndOfSearchNotReached() {
		boolean noMisMatchFoundYet = isPalindrome;
		boolean endOfSearchNotReached = index <= halfTheStringsLength();
		return noMisMatchFoundYet && endOfSearchNotReached;
	}

	private static int halfTheStringsLength() {

		float stringLength = aPossiblePalindrome.length();
		float halfItsLength = stringLength / 2;
		double halfItsLengthRoundedUp = Math.ceil(halfItsLength);

		return (int) halfItsLengthRoundedUp;

	}

	private static void checkIfCorrespondingCharsMatch() {
		if (correspondingCharsDontMatch(index)) {
			isPalindrome = false;
		}
	}

	private static boolean correspondingCharsDontMatch(int index) {

		char charCountingFromStartOfString = aPossiblePalindrome.charAt(index);
		char charCountingFromEndOfString = aPossiblePalindrome
				.charAt(palindromeLength - index - 1);
		boolean theyDontMatch = charactersMisMatch(
				charCountingFromStartOfString, charCountingFromEndOfString);

		return theyDontMatch;

	}

	private static boolean charactersMisMatch(
			char charCountingFromStartOfString, char charCountingFromEndOfString) {

		boolean theyDontMatch = charCountingFromStartOfString != charCountingFromEndOfString;

		return theyDontMatch;
	}

	private static String convertIntToString(int possiblePalindrome) {

		aPossiblePalindrome = getStringFromInteger(possiblePalindrome);

		return aPossiblePalindrome;

	}

	private static String getStringFromInteger(int aPalindrome) {
		return String.valueOf(aPalindrome);
	}

	private static boolean numberToCheckIsSingleDigit() {
		return numberToCheck <= SMALLEST_POSITIVE_SINGLE_DIGIT;
	}
}
